#pragma once


#include "Pages/EditorPageViewModel.h"
#include "EditorScenePageModel.h"
#include "Math/Vec3.h"
#include "Math/Quat.h"
#include "Math/Vec2.h"
#include "Math/Coordinate.h"
#include "EditorScenePageViewModel.m.h"

class RScene;
class RPixelObjectMap;
class RViewportWidget;

RCLASS()
class REditorScenePageViewModel : public REditorPageViewModel 
{
    ROBJECT

public:
    void BindModel(TSharedObjectPtr<REditorPageModel> InModel) override;
    TSharedObjectPtr<RTexture2D> GetIcon() const override;
    CString GetTitle() const override;

    //播放按钮的图标
    TSharedObjectPtr<RTexture2D>  GetPlayButtonIcon() const;
    //播放按钮的提示
    CString GetPlayButtonTooltip() ;
    void OnPlayButtonClicked();
    virtual float GetOperationsWidth() override;

    bool ShowAxis() const;
    SVec3 GetAxisPosition() const;
    SQuat GetAxisRotation() const;
    float GetAxisScale() const;

    SVec3 ViewPosition = SVec3(0, 0, 0);
    SQuat ViewRotation = SQuat(0, 0, 0, 1);

    void ViewMoveLeft();
    void ViewMoveRight();
    void ViewMoveForward();
    void ViewMoveBackward();
    void ViewMoveUp();
    void ViewMoveDown();


    void BeginMouseDrag(TSharedObjectPtr<RViewportWidget> InViewport);
    void OnMouseDrag(TSharedObjectPtr<RViewportWidget> InViewport,const SVec2 & TotalDelta);
    void EndMouseDrag();
protected:
    TSharedObjectPtr<RTexture2D> Icon;
    TWeakObjectPtr<REditorScenePageModel> Model;

    TSharedObjectPtr<RTexture2D> Play;
    TSharedObjectPtr<RTexture2D> Stop;

    const float MoveSpeed = 0.1f;

public:
    TSharedObjectPtr<RScene> GetScene() const;

protected:

    enum class EDragType
    {
        None, //无拖拽
        View, //拖拽视角
        MoveAlongAxis, //沿着轴移动
    };

    EDragType DragType = EDragType::None;

    //拖拽视角
    void BeginDragView();
    void OnDragView(const SVec2 & TotalDelta);
    SQuat InitRotationQuat;

    //沿着轴移动
    void BeginDragMoveAlongAxis(
        EAxis InAxis
        ,const SVec3& InMouseWorldPosition
        ,const SVec3& InMouseWorldDirection
    );
    void OnDragMoveAlongAxis(
        const SVec3& InMouseWorldPosition
        ,const SVec3& InMouseWorldDirection
    );
    SVec3 CalcDragPoint(
        const SVec3& InMouseWorldPosition
        , const SVec3& InMouseWorldDirection);
        
    SVec3 InitNodePosition;
    SVec3 InitDragPoint; //拖拽点
    SVec3 DragAxisDirection;
    SVec3 InitMouseWorldPosition;
    SVec3 InitMouseWorldDirection;

public:
    TSharedObjectPtr<RPixelObjectMap> GetPixelObjectMap(TSharedObjectPtr<RViewportWidget> InViewport);

protected:
    TSharedObjectPtr<RPixelObjectMap> PixelObjectMap;
    SVec2 ViewportFormerSize;
};
